package com.fan.teamer.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Team;

public class EventDaoImpl implements EventDao {
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Event getEvent(int id) {
		Event event = (Event) sessionFactory.getCurrentSession().get(
				Event.class, id);
		return event;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Event> getAllEvents() {
		return sessionFactory.getCurrentSession().createCriteria(Event.class)
				.list();
	}

	@Override
	public int getSize() {
		return getAllEvents().size();
	}

	@Override
	public void addEvent(Event event) {
		sessionFactory.getCurrentSession().save(event);
	}

	@Override
	public void updateEvent(Event event) {
		sessionFactory.getCurrentSession().update(event);
	}

	@Override
	public void deleteEvent(Event event) {
		sessionFactory.getCurrentSession().delete(event);
	}

	@Override
	public boolean hasEvent(int id) {
		if (getEvent(id) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public List<String> getColumnNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Event> getTeamEvents(Team team) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Event.class);
		criteria.add(Restrictions.eq("teamId", team.getId()));
		List<Event> events = (List<Event>) criteria.list();

		return events;
	}

}
